/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rg1tcpdemo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
 *
 * @author Administrator
 */
public class ServerListenRunnable implements Runnable
{
    private int serverPort;
    private ServerSocket serverSocket;
    private ArrayList<Socket> clientSockets;
    private JTextArea jTextArea1;

    public ServerListenRunnable(JTextArea jTextArea1)
    {
        this.jTextArea1 = jTextArea1;
        serverPort=5555;
        clientSockets=new ArrayList<>();
        try
        {
            serverSocket=new ServerSocket(serverPort); 
        } catch (IOException ex)
        {
            Logger.getLogger(ServerListenRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                Socket clientSocket= serverSocket.accept();
                System.out.println("�����������ӣ�"+clientSocket.toString());
                clientSockets.add(clientSocket);
                Thread readThread=new Thread(new SocketReadRunnable(clientSocket, jTextArea1));
                readThread.start();
            } catch (IOException ex)
            {
                Logger.getLogger(ServerListenRunnable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public ArrayList<Socket> getClientSockets()
    {
        return clientSockets;
    }
    
    public void closeServerSocket()
    {
        try {
            serverSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(ServerListenRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
}
